home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / bplus20.zip / NAMES.C < prev    next >
Text File  |  1987-11-12  |  7KB  |  227 lines

  1. /*******************************************************************/
  2. /*                              NAMES.C                             */
  3. /*                                                                  */
  4. /* This example shows how easy it is to write a program for an      */
  5. /* online address book using the B-PLUS file indexing toolkit.      */
  6. /* The program creates a file of names, addresses, and telephone    */
  7. /* numbers.  A record is displayed on the screen by entering part   */
  8. /* or all of the name.  Although the program is usefully as written */
  9. /* it has purposely been kept simple.  You may want to add new      */
  10. /* features to the progran.                                         */
  11. /*                                                                  */
  12. /********************************************************************/
  13.  
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include "bplus.h"
  18.  
  19.  
  20. typedef struct              /* Here is the address record definition */
  21.   {
  22.      char lastname[16];     /* last name           */
  23.      char firstname[16];    /* first name          */
  24.      char address1[31];     /* first address line  */
  25.      char address2[31];     /* second address line */
  26.      char city[21];         /* the city            */
  27.      char state[3];         /* the state           */
  28.      char zipcode[6];       /* postal zip code     */
  29.      char telephone[14];    /* telephone number    */
  30.   }  ADDRESS;
  31.  
  32.  
  33. IX_DESC  nameindex;             /* index file variable  */
  34. FILE     *namefile;             /* data file pointer    */
  35. ADDRESS  person;                /* data record variable */
  36.  
  37.  
  38. void openfiles(void);
  39. void closefiles(void);
  40. int addrecord(void);
  41. void getstring(char*, int);
  42. void newaddress(void);
  43. void printname(ENTRY*);
  44. void getname(void);
  45. void nextname(void);
  46. void listnames(void);
  47.  
  48.  
  49. void openfiles()
  50.   /* If the file NAMES.DAT already exist, open the index and data */
  51.   /* file.  Otherwise, these files are created.                   */
  52.   {
  53.     if ((namefile = fopen("names.dat","r+")) != NULL)
  54.       open_index("names.idx", &nameindex, 1);      /* open index file  */
  55.     else
  56.     {
  57.       namefile = fopen("names.dat","w+");          /* create data file */
  58.       if (namefile == NULL)
  59.         {
  60.           printf("Unable to open namefile\n");
  61.           exit(1);
  62.         }
  63.       make_index("names.idx", &nameindex, 1);   /* creat index file     */
  64.     }                                           /* allow duplicate keys */
  65.   } /* openfiles */
  66.  
  67.  
  68. void closefiles()
  69.   /* close all files and exit */
  70.   {
  71.     fclose(namefile);
  72.     close_index(&nameindex);
  73.     exit(0);
  74.   } /* closefiles */
  75.  
  76.  
  77. int addrecord()
  78.   /* add a new address to the data file - add index to index file */
  79.   {
  80.     ENTRY ee;
  81.     char name[32];
  82.     int ret;
  83.     ret = fseek(namefile, 0L, SEEK_END);      /* seek to end of datafile  */
  84.     if (ret == 0)
  85.       {
  86.         strcpy(ee.key, person.lastname);      /* key is last name followed */
  87.         strcat(ee.key, person.firstname);     /* first name.  Capitalize   */
  88.         strupr(ee.key);                       /*    and copy to ee.key.    */
  89.         ee.recptr = ftell(namefile);          /* get position in datafile  */
  90.         if (ee.recptr != -1L)
  91.           {
  92.             if (add_key(&ee, &nameindex) == IX_OK)     /* add key to index */
  93.               {
  94.                 fwrite(&person,sizeof(person),1,namefile);  /* add address */
  95.                 return (IX_OK);
  96.               }
  97.           }
  98.       }
  99.     else printf("Seek error - data file");
  100.     return (IX_FAIL);
  101.   } /* addrecord */
  102.  
  103.  
  104. void getstring(mes, length)
  105.   char *mes;
  106.   int length;
  107.   /* input a string and check that it is not too long   */
  108.   {
  109.     char message[80];
  110.     gets(message);
  111.     if (strlen(message) > length) message[length] = '\0';
  112.     strcpy(mes,message);
  113.   } /* getstring */
  114.  
  115.  
  116. void newaddress()
  117.   /* add new address records */
  118.   {
  119.     while (1)
  120.      {
  121.        printf("\n\nLast Name      : ");
  122.        getstring(person.lastname,15);
  123.        if ( strlen(person.lastname) > 0)       /* quit if no last name */
  124.          {
  125.            printf("First Name     : ");
  126.            getstring(person.firstname,15);
  127.            printf("Address Line 1 : ");
  128.            getstring(person.address1,30);
  129.            printf("Address Line 2 : ");
  130.            getstring(person.address2,30);
  131.            printf("City           : ");
  132.            getstring(person.city,20);
  133.            printf("State          : ");
  134.            getstring(person.state,2);
  135.            printf("Zip Code       : ");
  136.            getstring(person.zipcode,5);
  137.            printf("Telephone      : ");
  138.            getstring(person.telephone,13);
  139.            addrecord();                     /* update data and index files */
  140.            printf("\n");
  141.          }
  142.          else return ;
  143.      }
  144.   } /* newaddress */
  145.  
  146.  
  147. void printname(e)
  148.   ENTRY *e;
  149.   /* retrieve a data record and print it on the screen */
  150.   {
  151.     int ret;
  152.  
  153.     /* seek to the record address stored in ENTRY e->recptr */
  154.     ret = fseek(namefile, e->recptr, SEEK_SET);
  155.  
  156.     if (ret == 0)     /* if OK read the record and display */
  157.       {
  158.         fread(&person,sizeof(person),1,namefile);
  159.         printf("\n\n            %s %s" , person.firstname,person.lastname);
  160.         printf("\n            %s", person.address1);
  161.         if (strlen(person.address2) > 0)
  162.            printf("\n            %s", person.address2);
  163.         printf("\n            %s, %s  %s", person.city,person.state,person.zipcode);
  164.         printf("\n            %s\n", person.telephone);
  165.       }
  166.     else printf("Seek error - data file");
  167.   } /* printname */
  168.  
  169.  
  170. void getname()
  171.   /* Get an address record by entering part or all of name */
  172.   /* Enter last name first then first name with no spaces  */
  173.   {
  174.     ENTRY ee;
  175.     printf("\n\nEnter name: ");
  176.     gets(ee.key);
  177.  
  178.     /* make all upper case letters and copy to ee.key */    
  179.     strupr(ee.key);
  180.  
  181.     /* use locate_key instead of find_key so an exact match not required */
  182.     if (locate_key(&ee, &nameindex) != EOIX) printname(&ee);
  183.     else printf("No key this large in index file\n");
  184.   } /* getname */
  185.  
  186.  
  187. void nextname()
  188.   /* display the next address in the address file */
  189.   {
  190.     ENTRY ee;
  191.     if (next_key(&ee, &nameindex) == IX_OK) printname(&ee);
  192.     else printf("\nEnd of index file\n");
  193.   } /* nextname */
  194.  
  195.  
  196. void listnames()
  197.   /* list all the names in the address file */
  198.   {
  199.     ENTRY ee;
  200.     first_key(&nameindex);
  201.     while (next_key(&ee, &nameindex) == IX_OK) printname(&ee);
  202.   } /* listnames */
  203.  
  204. main()
  205.   /* Here is the main program loop */
  206.   {
  207.     char cmd;
  208.     int  done;
  209.     done = 0;
  210.     openfiles();
  211.     do
  212.      {
  213.        printf("\nCommand: A (Add Name), F (Find), N (Next), L (List), Q (Quit): ");
  214.        cmd = toupper(getche());
  215.        switch (cmd)
  216.         {
  217.           case 'A': newaddress(); break;   /* add a name to address file   */
  218.           case 'F': getname(); break;      /* find an address              */
  219.           case 'N': nextname(); break;     /* display next address in file */
  220.           case 'L': listnames(); break;    /* display all addresses        */
  221.           case 'Q': closefiles();          /* quit and close files         */
  222.         }
  223.       }
  224.     while (!done);
  225.   } /* main */
  226.  
  227.